﻿/*
 * Created by SharpDevelop.
 * User: wanggy
 * Date: 2009/09/28
 * Time: 13:12
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using XRunner.SDK.XUtill;
using System.Net;
using System.Text.RegularExpressions;
using System.Data;
using System.Xml;

namespace XRunner
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
	    private CookieContainer cc = new CookieContainer();
        private string _uid = string.Empty;
        private string verify = string.Empty;
        private string xmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
        private List<string> seedIds = new List<string>();
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}

        private void button1_Click(object sender, EventArgs e)
        {
            string url = "http://www.kaixin001.com/login/login.php";
            string postData = string.Format("url=/home/&invisible_mode=0&email={0}&password={1}", email.Text, pwd.Text);
            string rlt = HTTPUtill.doSend(url, postData, "POST", cc);
            result.Text = rlt;
            CookieCollection ckc = cc.GetCookies(new Uri(url));
            _uid = ckc["_uid"].ToString();
        }
		
		void BtnHouseSendClick(object sender, EventArgs e)
		{
			string url = "http://www.kaixin001.com/!house/index.php?_lgmode=pri&t=2";
            string regex = "var verify = \"(.*)\"";
            string rst = HTTPUtill.doSend(url, string.Empty, "GET", cc);
            txtHouseResult.Text = rst;
            Regex reg = new Regex(regex);
            MatchCollection mc = reg.Matches(rst);
            if (1 == mc.Count)
            {
                 verify = mc[0].Groups[1].ToString();
            }
		}

        private void btnSeedInfo_Click(object sender, EventArgs e)
        {
        	string url = string.Format("http://www.kaixin001.com/!house/!garden//seedlist.php?verify={0}",verify);
            string rst = HTTPUtill.doSend(url, string.Empty, "GET", cc);
            DataSet ds = new DataSet();
            ds.ReadXml(new System.IO.StringReader(rst));
            DataTable dt = ds.Tables["item"];

            Hashtable titles = new Hashtable();
            titles.Add("name", "种子名字");
            //titles.Add("fruitpic", "2");
            titles.Add("seedid", "种子ID");
            titles.Add("price", "价格");
            titles.Add("isnew", "新产品");
            //titles.Add("adinfo", "6");
            titles.Add("extprice", "格外价格");
            //titles.Add("beannum_str", "8");
            //titles.Add("beannum", "9");
            for(int index = 0; index < dt.Rows.Count; index++)
            {
            	seedIds.Add(dt.Rows[index]["seedid"].ToString());
            }

            reportUtil.createReportView(dt,lstvSeedInfo, titles);
        }
		
		void Button1Click(object sender, EventArgs e)
		{
			DataTable dt = new DataTable();
			for(int index = 0; index < seedIds.Count; index++)
			{
				string url = string.Format(
					"http://www.kaixin001.com/!house/!garden//seedinfo.php?seedid={0}&verify={1}",
				    seedIds[index],verify);
				string rst = HTTPUtill.doSend(url, string.Empty, "GET", cc);
				if (!rst.StartsWith(xmlHeader))
				{
					rst = xmlHeader + rst;
				}
				DataSet ds = new DataSet();
				ds.ReadXml(new System.IO.StringReader(rst));
				dt.Merge(ds.Tables["data"]);
			}
			reportUtil.createReportView(dt, listSeedDetail, null);
		}
	}
}
